﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class CmsModel
    {
        public int Add(KeRui.Model.CmsModel model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ModelName", SqlDbType.NVarChar, 50), new SqlParameter("@ModelType", SqlDbType.Int, 4), new SqlParameter("@Description", SqlDbType.NVarChar, 900), new SqlParameter("@ItemName", SqlDbType.NVarChar, 50), new SqlParameter("@ItemUnit", SqlDbType.NVarChar, 50), new SqlParameter("@IsUsing", SqlDbType.Bit, 1), new SqlParameter("@OrderID", SqlDbType.Int) };
            parameters[0].Value = model.ModelName;
            parameters[1].Value = model.ModelType;
            parameters[2].Value = model.Description;
            parameters[3].Value = model.ItemName;
            parameters[4].Value = model.ItemUnit;
            parameters[5].Value = model.IsUsing;
            parameters[6].Value = model.OrderID;
            return DbHelperSQL.RunProcedure("CmsModel_ADD", parameters, out num);
        }

        public int Delete(int ModelID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ModelID", SqlDbType.Int, 4) };
            parameters[0].Value = ModelID;
            return DbHelperSQL.RunProcedure("CmsModel_Delete", parameters, out num);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ModelID,ModelName,ModelType,Description,ItemName,ItemUnit,IsUsing,OrderID,CreateTime ");
            builder.Append(" FROM CmsModel ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ModelID,ModelName,ModelType,Description,ItemName,ItemUnit,IsUsing,OrderID,CreateTime ");
            builder.Append(" FROM CmsModel ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public KeRui.Model.CmsModel GetModel(int ModelID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ModelID", SqlDbType.Int, 4) };
            parameters[0].Value = ModelID;
            KeRui.Model.CmsModel model = new KeRui.Model.CmsModel();
            DataSet set = DbHelperSQL.RunProcedure("CmsModel_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["ModelID"].ToString() != "")
                {
                    model.ModelID = int.Parse(set.Tables[0].Rows[0]["ModelID"].ToString());
                }
                model.ModelName = set.Tables[0].Rows[0]["ModelName"].ToString();
                if (set.Tables[0].Rows[0]["ModelType"].ToString() != "")
                {
                    model.ModelType = int.Parse(set.Tables[0].Rows[0]["ModelType"].ToString());
                }
                model.Description = set.Tables[0].Rows[0]["Description"].ToString();
                model.ItemName = set.Tables[0].Rows[0]["ItemName"].ToString();
                model.ItemUnit = set.Tables[0].Rows[0]["ItemUnit"].ToString();
                if (set.Tables[0].Rows[0]["IsUsing"].ToString() != "")
                {
                    if ((set.Tables[0].Rows[0]["IsUsing"].ToString() == "1") || (set.Tables[0].Rows[0]["IsUsing"].ToString().ToLower() == "true"))
                    {
                        model.IsUsing = true;
                    }
                    else
                    {
                        model.IsUsing = false;
                    }
                }
                if (set.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    model.OrderID = int.Parse(set.Tables[0].Rows[0]["OrderID"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return model;
            }
            return null;
        }

        public int Update(KeRui.Model.CmsModel model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ModelID", SqlDbType.Int, 4), new SqlParameter("@ModelName", SqlDbType.NVarChar, 50), new SqlParameter("@ModelType", SqlDbType.Int, 4), new SqlParameter("@Description", SqlDbType.NVarChar, 900), new SqlParameter("@ItemName", SqlDbType.NVarChar, 50), new SqlParameter("@ItemUnit", SqlDbType.NVarChar, 50), new SqlParameter("@IsUsing", SqlDbType.Bit, 1), new SqlParameter("@OrderID", SqlDbType.Int, 4) };
            parameters[0].Value = model.ModelID;
            parameters[1].Value = model.ModelName;
            parameters[2].Value = model.ModelType;
            parameters[3].Value = model.Description;
            parameters[4].Value = model.ItemName;
            parameters[5].Value = model.ItemUnit;
            parameters[6].Value = model.IsUsing;
            parameters[7].Value = model.OrderID;
            return DbHelperSQL.RunProcedure("CmsModel_Update", parameters, out num);
        }
    }
}

